/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alee.laf.tree;

import com.alee.utils.MapUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * This object might contain tree selection and expand states for all loaded tree nodes.
 * Usually this object is constructed through TreeUtils automatically and could be applied to some tree using these utilities aswell.
 *
 * @author Mikle Garin
 * @see TreeUtils
 * @since 1.4
 */

// todo Custom converter to simplify xml
public final class TreeState implements Serializable, Cloneable
{
    /**
     * Tree node expand states by node indices.
     */
    private Map<String, Boolean> expandStates = new HashMap<String, Boolean> ();

    /**
     * Tree node selection state by node indices.
     */
    private Map<String, Boolean> selectionStates = new HashMap<String, Boolean> ();

    /**
     * Constructs new object instance with empty states.
     */
    public TreeState ()
    {
        super ();
    }

    /**
     * Constructs new object instance with specified states.
     */
    public TreeState ( Map<String, Boolean> expandStates, Map<String, Boolean> selectionStates )
    {
        super ();
        if ( expandStates != null )
        {
            setExpandStates ( expandStates );
        }
        if ( selectionStates != null )
        {
            setSelectionStates ( selectionStates );
        }
    }

    /**
     * Returns expand states.
     *
     * @return expand states
     */
    public Map<String, Boolean> getExpandStates ()
    {
        return expandStates;
    }

    /**
     * Sets expand states.
     *
     * @param expandStates new expand states
     */
    public void setExpandStates ( Map<String, Boolean> expandStates )
    {
        this.expandStates = expandStates;
    }

    /**
     * Adds single expand state.
     *
     * @param id    unique node id
     * @param state expand state
     */
    public void addExpandState ( String id, Boolean state )
    {
        expandStates.put ( id, state );
    }

    /**
     * Returns selection states.
     *
     * @return selection states
     */
    public Map<String, Boolean> getSelectionStates ()
    {
        return selectionStates;
    }

    /**
     * Sets selection states.
     *
     * @param selectState new selection states
     */
    public void setSelectionStates ( Map<String, Boolean> selectState )
    {
        this.selectionStates = selectState;
    }

    /**
     * Adds single selection state.
     *
     * @param id    unique node id
     * @param state selection state
     */
    public void addSelectState ( String id, Boolean state )
    {
        selectionStates.put ( id, state );
    }

    /**
     * Returns cloned states object.
     *
     * @return cloned states object
     */
    public TreeState clone ()
    {
        return new TreeState ( MapUtils.cloneMap ( expandStates ), MapUtils.cloneMap ( selectionStates ) );
    }
}